package com.cpi.cornpp.costelement.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.costelement.dto.CostElementDTO;
import com.cpi.cornpp.costelement.view.CostElementView;

public class CostElementConverter {
	/**
	 * converter DTO to View
	 * @param costElementDTO
	 * @return
	 */
	public CostElementView convert(CostElementDTO costElementDTO){
		CostElementView costElementView = null;
		
		if (costElementDTO != null){
			costElementView = new CostElementView();			
			costElementView.setCostElementID(costElementDTO.getCostElementID());			
			costElementView.setCostElementCode(costElementDTO.getCostElementCode());
			costElementView.setCostElementDescription(costElementDTO.getCostElementDescription());
			costElementView.setTypeId(costElementDTO.getTypeId());
		}
		return costElementView;
	}
	/**
	 * Convert View to DTO
	 * @param costElementView
	 * @return
	 */
	public CostElementDTO convertViewToDTO(CostElementView costElementView){
		CostElementDTO costElementDTO = null;
		
		if (costElementView != null){
			costElementDTO = new CostElementDTO();			
			costElementDTO.setCostElementID(costElementView.getCostElementID());			
			costElementDTO.setCostElementCode(costElementView.getCostElementCode());
			costElementDTO.setCostElementDescription(costElementView.getCostElementDescription());
		}
		return costElementDTO;
	}	
	/**
	 * Convert the View to DTO
	 * @param costElementDTO
	 * @return
	 */
	public List convertDTOs(Collection costElementsDTO){
		List resultList = null;
		resultList = new ArrayList(0);
		Iterator iterator = costElementsDTO.iterator();
		CostElementDTO costElementDTO = null;
		if (!CollectionUtils.isEmpty(costElementsDTO)){
			while( iterator.hasNext() ){
				costElementDTO = (CostElementDTO)iterator.next();
				resultList.add(convert(costElementDTO));
			}
		}
		
		return resultList;
	}
	
}
